package com.tavio.tbs.scene;

import java.util.HashSet;
import java.util.Set;

import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.Sprite;
import org.andengine.extension.tmx.TMXTile;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.TextureManager;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import android.content.res.AssetManager;

import com.tavio.tbs.sprite.SelectionRectSprite;
import com.tavio.tbs.structure.Position;
import com.tavio.tbs.unit.Unit;
import com.tavio.tbs.world.WorldMap;

public class SceneManager {
	
	private static final Scene scene = new Scene();
	
	private static SceneManager instance = null;
	
	private Set<SelectionRectSprite> selectionRects;
 	
	private VertexBufferObjectManager vertexBufferObjectManager;
	private TextureManager textureManager;
	private BitmapTextureAtlas mBitmapTextureAtlas;
	private ITexture selectionRectTexture;
	private ITextureRegion selectionRectTextureRegion;
	private AssetManager assetManager;
	
	private SceneManager() {}
	
	public static SceneManager getInstance() {
        if(instance == null)
        	instance = new SceneManager();
		return instance;
	}

	public Scene getScene() {
		return scene;
	}
	
	public void init(VertexBufferObjectManager vertexBufferObjectManager, TextureManager textureManager, AssetManager assetManager) {
		this.vertexBufferObjectManager = vertexBufferObjectManager;
		this.textureManager = textureManager;
		this.assetManager = assetManager;
		
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		this.mBitmapTextureAtlas = new BitmapTextureAtlas(textureManager, 40, 40, TextureOptions.BILINEAR);
		this.selectionRectTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, assetManager, "selection_rect.png", 0, 0, 1, 1);
		this.mBitmapTextureAtlas.load();
		
		this.selectionRects = new HashSet<SelectionRectSprite>();
	}
	
	public void drawSelectionRectangleOverTile(Position position, float percentageWidth, float percentageHeight, Unit responsibleUnit) {
		SelectionRectSprite rect = new SelectionRectSprite(position, percentageWidth * WorldMap.getInstance().getTileWidth(), percentageHeight * WorldMap.getInstance().getTileHeight(), selectionRectTextureRegion, vertexBufferObjectManager, responsibleUnit);
		rect.setZIndex(1);
		scene.attachChild(rect);
		selectionRects.add(rect);
		scene.sortChildren();
	}
	
	public void drawSpriteOverTile(Position position, Sprite sprite) {
		TMXTile tile = WorldMap.getInstance().getTmxLayer(0).getTMXTile(position.getX(), position.getY());
		sprite.setPosition(tile.getTileX() + WorldMap.getInstance().getTileWidth()/2 - sprite.getWidth() / 2 , tile.getTileY() + WorldMap.getInstance().getTileHeight()/2 - sprite.getHeight() / 2);
	}
	
	public void removeSelectionRects() {
		
		for(Sprite s : selectionRects) {
			SceneManager.getInstance().getScene().unregisterTouchArea(s);
			scene.detachChild(s);
			s.dispose();
			s = null;
		}
		selectionRects.clear();
	}
	
}
